// Package messages implements Kerberos 5 message types and methods.
package messages import ( ) // KRBError implements RFC 4120 KRB_ERROR: https://tools.ietf.org/html/rfc4120#section-5.9.1. type KRBError struct { PVNO int `asn1:"explicit,tag:0"` MsgType int `asn1:"explicit,tag:1"` CTime time.Time `asn1:"generalized,optional,explicit,tag:2"` Cusec int `asn1:"optional,explicit,tag:3"` STime time.Time `asn1:"generalized,explicit,tag:4"` Susec int `asn1:"explicit,tag:5"` ErrorCode int32 `asn1:"explicit,tag:6"` CRealm string `asn1:"generalstring,optional,explicit,tag:7"` CName types.PrincipalName `asn1:"optional,explicit,tag:8"` Realm string `asn1:"generalstring,explicit,tag:9"` SName types.PrincipalName `asn1:"explicit,tag:10"` EText string `asn1:"generalstring,optional,explicit,tag:11"` EData []byte `asn1:"optional,explicit,tag:12"` } // NewKRBError creates a new KRBError. func ( types.PrincipalName, string, int32, string) KRBError { := time.Now().UTC() return KRBError{ PVNO: iana.PVNO, MsgType: msgtype.KRB_ERROR, STime: , Susec: int((.UnixNano() / int64(time.Microsecond)) - (.Unix() * 1e6)), ErrorCode: , SName: , Realm: , EText: , } } // Unmarshal bytes b into the KRBError struct. func ( *KRBError) ( []byte) error { , := asn1.UnmarshalWithParams(, , fmt.Sprintf("application,explicit,tag:%v", asnAppTag.KRBError)) if != nil { return krberror.Errorf(, krberror.EncodingError, "KRB_ERROR unmarshal error") } := msgtype.KRB_ERROR if .MsgType != { return krberror.NewErrorf(krberror.KRBMsgError, "message ID does not indicate a KRB_ERROR. Expected: %v; Actual: %v", , .MsgType) } return nil } // Marshal a KRBError into bytes. func ( *KRBError) () ([]byte, error) { , := asn1.Marshal(*) if != nil { return , krberror.Errorf(, krberror.EncodingError, "error marshaling KRBError") } = asn1tools.AddASNAppTag(, asnAppTag.KRBError) return , nil } // Error method implementing error interface on KRBError struct. func ( KRBError) () string { := fmt.Sprintf("KRB Error: %s", errorcode.Lookup(.ErrorCode)) if .EText != "" { = fmt.Sprintf("%s - %s", , .EText) } return } func processUnmarshalReplyError( []byte, error) error { switch .(type) { case asn1.StructuralError: var KRBError := .Unmarshal() if != nil { return krberror.Errorf(, krberror.EncodingError, "failed to unmarshal KDC's reply") } return default: return krberror.Errorf(, krberror.EncodingError, "failed to unmarshal KDC's reply") } }